//	ActuatorDataSender.java --- 

package fr.upmc.dtgui.example.gui;

import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.BlockingQueue;
import javax.swing.SwingUtilities;

import fr.upmc.dtgui.example.robot.RobotActuatorCommand;

/**
 * The class <code>ActuatorDataSender</code> implements threads used to send
 * actuator commands to the controlled robot through its actuator command
 * blocking queue.  The thread schedules the addition of the new command into
 * the blocking queue as a task in the Swing event processing loop, waits for
 * the execution of this task and then dies.  Each time a new command is
 * inserted in the blocking queue, the queue is cleared of any unprocessed
 * data to always favor the freshness of data rather than the whole history
 * of data sent.
 *
 * <p><strong>Invariant</strong></p>
 * 
 * <pre>
 * invariant	true
 * </pre>
 * 
 * <p>Created on : 2011-09-19</p>
 * 
 * @author	<a href="mailto:Jacques.Malenfant@lip6.fr">Jacques Malenfant</a>
 * @version	$Name$ -- $Revision$ -- $Date$
 */
public class ActuatorDataSender extends Thread {

	protected RobotActuatorCommand	rac ;
	protected BlockingQueue<RobotActuatorCommand> commandQueue ;

	public ActuatorDataSender(RobotActuatorCommand rac,
			BlockingQueue<RobotActuatorCommand> commandQueue) {
		super();
		this.rac = rac;
		this.commandQueue = commandQueue;
	}

	@Override
	public void run() {
		try {
			SwingUtilities.invokeAndWait(
				new Runnable() {
					public void run() {
						commandQueue.clear() ;
						commandQueue.add(rac) ; }
				}) ;
		} catch (InterruptedException e1) {
			e1.printStackTrace();
		} catch (InvocationTargetException e1) {
			e1.printStackTrace();
		}
	}

}

// $Id$